package com.huarui.part.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.part.dao.IPartDao;
import com.huarui.part.model.Part;
import com.huarui.partner.model.Partner;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class PartDao extends BaseDaoImpl implements IPartDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Part> getPartListModel() {

		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getPartPage");
	}

	@Override
	public String getPartNameByCode(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Part> getPartListByConditions(Map map) {
		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getPartListByConditions", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Part getPartByConditions(Map map) {
		List<Part> list = this.getPartListByConditions(map);
		return list.size() > 0 ? list.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map getPartByConditionsJson(Map map) {

		return (Map) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartByConditionsJson", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Part> getAllParts(Map map) {

		return getSqlMapClientTemplate().queryForList(
				namespace + ".getAllParts", map);
	}

	@Override
	public void batchInsert(final List<Part> list) {
		@SuppressWarnings("unused")
		SqlMapClientCallback callback = new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				int batch = 0;
				for (Part part : list) {
					executor.insert(namespace + ".create", part);
					batch++;
					if (batch == 500) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		};
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Part> getAllComboxPart(Map map) {

		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getAllPartPage", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getPartListCountByConditions(Map map) {

		Integer count = (Integer) this.getSqlMapClientTemplate()
				.queryForObject(namespace + ".getPartListCountByConditions",
						map);
		return count;
	}

	@Override
	public Part getPartByCodeAndSource(String code, int source,
			Integer companyId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("source", source);
		map.put("companyId", companyId);

		return (Part) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartByCodeAndSource", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer getcountByCondition(Map map) {

		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".getcountByCondition", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getPartsForMapByConditions(Map map) {
		return getSqlMapClientTemplate().queryForList(
				namespace + ".getPartsForMapByConditions", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getPartCodeListByConditions(Map<String, Object> map) {
		return getSqlMapClientTemplate().queryForList(
				namespace + ".getPartCodeListByConditions", map);
	}

	@Override
	public Integer getPartCodeCountByConditions(Map<String, Object> map) {
		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartCodeCountByConditions", map);
	}

	@Override
	public Partner getPartnerByIdent(Map map) {

		return (Partner) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartnerByIdent", map);
	}

	@Override
	public Partner getPartnerByTaxCode(String taxCode) {

		return (Partner) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartnerByTaxCode", taxCode);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Part getPartByCode(String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);

		List<Part> list = getSqlMapClientTemplate().queryForList(
				namespace + ".getPartByCode", map);

		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}

	}

}
